/**
 * 
 */
package fgsz;

import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.ss.usermodel.DataFormatter;

import java.util.Date;
import java.util.Iterator;

import java.io.*;


/**
 * @author Bordas Akos
 *
 */

public abstract class XlsxReader {

	private FileInputStream myFile = null;
	private XSSFWorkbook myWorkbook;
	private XSSFSheet mySheet;

	public XlsxReader(String aFilePath)
	{
		try
		{
			myFile = new FileInputStream(aFilePath);
			myWorkbook = new XSSFWorkbook(myFile);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * @param XSSFCell cell
	 * @description The purpose of this method to determine if the cell value is string or not. If that is empty it will return false else true.  
	 */
	public boolean isStringValue(XSSFCell cell)
	{
		if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * @param XSSFCell cell
	 * @description The purpose of this method to determine if the cell value is numeric or not. If that is empty it will return false else true.  
	 */
	public boolean isNumericValue(XSSFCell cell)
	{
		if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * @param XSSFCell cell
	 * @description The purpose of this method to determine if the cell value is empty or not. If that is empty it will return false else true.  
	 */
	public boolean isBlankValue(XSSFCell cell)
	{
		if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * @param int aSheetNumber
	 * @description The purpose of this method to set the sheet by the given name.
	 */	
	public void setSheet(String aSheetName)
	{
		int numofsheets = myWorkbook.getNumberOfSheets();
		XSSFSheet sheet;
		for (int i = 0; i < numofsheets; i++)
		{
			sheet = myWorkbook.getSheetAt(i);
			String sname = sheet.getSheetName();
			if (sname == aSheetName)
			{
				mySheet = myWorkbook.getSheetAt(i);
			}
		}
	}

	/**
	 * @param int aSheetNumber
	 * @description The purpose of this method to set the sheet by the given number. E.g. the first sheet is 0, the second is 1...etc.
	 */
	public void setSheet(int aSheetNumber)
	{
		mySheet = myWorkbook.getSheetAt(aSheetNumber);
	}

	/*
	public XSSFCell getColumnByName(XSSFCell cell, String CoumnName)
	{
		CoumnName.toUpperCase();
		String columnNames[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","V","W","X","Y","Z"};
	}
	*/

	/**
	 * @param XSSFCell cell
	 * @description The purpose of this method to determine if the cell number formatted cell is bigger than 0.
	 * If the value bigger than 0 it returns true, else return false. 
	 */
	public boolean isColumnEmpty(XSSFCell cell)
	{
		 if (cell.getCellType() != 3)
		 {
			 return false;
		 }
		 else
		 {
			 return true;
		 }
	}
	
	/**
	 * @param XSSFCell cell
	 * @description The purpose of this method to determine if the cell number formatted cell is bigger than 0.
	 * If the value bigger than 0 it returns true, else return false. If the cell has not numeric type, then it returns 0.
	 */
	public boolean isCellBiggerZero(XSSFCell cell)
	{
		if (isNumericValue(cell))
		{
			double numberCell = cell.getNumericCellValue();
			if (numberCell < 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * @description The purpose of this method to be overridden by the child classes. 
	 * So that it can be used as a specified method in each subclasses. It will accomplish the xlsx file reading by specified cases.
	 */
	public abstract void Reading();
	
}
